<template>
    <div>
        <el-input v-model="findInfo" placeholder="请输入想查询的景点" style="width: 200px;margin: 10px 0;"></el-input><el-button
            style="margin:0 0 0 10px;" @click="findSpots()">点击搜索</el-button>
        <el-table :data="spotsList" style="width: 100%;">
            <el-table-column label="名称" prop="spot_name">
            </el-table-column>
            <el-table-column label="地址" prop="spot_site">
            </el-table-column>
            <el-table-column label="等级" prop="spot_level" show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="开放时间" prop="spot_time" show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="票价" prop="spot_rates">
            </el-table-column>
            <el-table-column label="介绍" prop="spot_address" show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column align="right" width="150">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row._id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination style="width:50%;margin: 20px auto 0;" background @size-change="handleSizeChange"
            @current-change="handleCurrentChange" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize" layout=" prev, pager, next, jumper,total, sizes" :total="total">
        </el-pagination>



        <el-dialog title="信息修改" :visible.sync="dialogFormVisible" width="25%">
            <el-form :model="form" ref="ruleForm">
                <el-form-item label="名称" :label-width="formLabelWidth" prop="spot_name">
                    <el-input v-model="form.spot_name" autocomplete="off" placeholder="景点名称"></el-input>
                </el-form-item>
                <el-form-item label="地址" :label-width="formLabelWidth" prop="spot_site">
                    <el-input v-model="form.spot_site" autocomplete="off" placeholder="地址"></el-input>
                </el-form-item>
                <el-form-item label="等级" :label-width="formLabelWidth" prop="spot_level">
                    <el-input v-model="form.spot_level" autocomplete="off" placeholder="等级"></el-input>
                </el-form-item>
                <el-form-item label="开放时间" :label-width="formLabelWidth" prop="spot_time">
                    <el-input v-model="form.spot_time" autocomplete="off" placeholder="开放时间"></el-input>
                </el-form-item>
                <el-form-item label="票价" :label-width="formLabelWidth" prop="spot_rates">
                    <el-input v-model="form.spot_rates" autocomplete="off" placeholder="票价"></el-input>
                </el-form-item>
                <el-form-item label="介绍" :label-width="formLabelWidth" prop="spot_address">
                    <el-input type="textarea" v-model="form.spot_address" autocomplete="off" placeholder="介绍"
                        style="min-height: 80px !important;" maxlength="1000" show-word-limit
                        showOverflowTooltip></el-input>
                </el-form-item>
                <el-form-item label="图片信息" :label-width="formLabelWidth">
                    <el-upload style="margin:10px 0px!important;" :action="uploadUrl" list-type="picture-card"
                        :on-preview="handlePictureCardPreview" :on-remove="handleRemove" :headers="headerObj"
                        :on-success="handleSuccess" :file-list="fileList">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import api from '@/api'
export default {
    mounted() {
        this.getSpots();
    },
    data() {
        return {
            pageIndex: 1,
            pageSize: 10,
            total: 10,
            spotsList: [],
            updataimg: [],
            findInfo: '',
            dialogFormVisible: false,
            form: {
                _id: "",
                spot_name: "",
                spot_site: "",
                spot_address: "",
                spot_level: "",
                spot_time: "",
                spot_rates: "",
                spot_img: []
            },
            headerObj: {
                file: '',
            },
            //1.配置要上传文件的路由
            uploadUrl: "http://localhost:4000/file/uploadImg",
            fileList: [],
            dialogImageUrl: '',
            dialogVisible: false,
            disabled: false,
            formLabelWidth: '80px'
        }
    },
    watch: {
        findInfo(newVal, oldVal) {
            if (oldVal !== '' && newVal === '') {
                this.getSpots();
            }
        }
    },
    methods: {
        /**上传成功后将图片放入表单 */
        handleSuccess(response, file, fileList) {
            this.updataimg.push(response.url)
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        /**图片删除时对上传的图片也做修改 */
        handleRemove(file) {
            let index = this.updataimg.indexOf(file.response.url);
            if (index !== -1) {
                this.updataimg.splice(index, 1);
            }
        },
        async getSpots() {
            let res = await api.getSpots({ pageIndex: this.pageIndex, pageSize: this.pageSize })
            if (res?.code == 200) {
                this.spotsList = res.data;
                this.total = res.total
            } else {
                this.spotsList = []
            }
        },
        handleEdit(userInfo) {
            this.fileList = [];
            this.dialogFormVisible = true
            this.form = { ...userInfo }
        },
        /**删除函数 */
        async handleDelete(spot_id) {
            let res = await api.delSports({ spot_id });
            if (res?.code == 200) {
                this.$message({
                    message: res.message,
                    type: 'success'
                });
                this.getSpots();
            } else {
                this.$message.error(res.message);
            }
        },
        async handleOk() {
            if (this.updataimg.length == 0) {
                this.$message.error('请上传图片');
            } else {
                this.fileList = [];
                this.dialogFormVisible = false;
                this.form.spot_img = this.updataimg
                let res = await api.updateSpots({ ...this.form })
                if (res?.code == 200) {
                    this.$message({
                        message: res.message,
                        type: 'success'
                    });
                    this.getSpots();
                } else {
                    this.$message.error(res.message);
                }
            }
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.getSpots()
        },
        handleCurrentChange(val) {
            this.pageIndex = val;
            this.getSpots()
        },
        submitForm(formName) {
            this.fileList = [];
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.handleOk();
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.fileList = [];
            this.$refs[formName].resetFields();
        },
        /**搜索函数 */
        async findSpots() {
            if (this.findInfo === '') {
                this.getSpots()
            } else {
                let res = await api.getSpots({ pageIndex: this.pageIndex, pageSize: this.pageSize })
                this.spotsList = []
                if (res?.code == 200) {
                    for (let i = 0; i < res.data.length; i++) {
                        if (this.findInfo === res.data[i].spot_name || this.findInfo === res.data[i].spot_site || this.findInfo === res.data[i].spot_cityid) {
                            this.spotsList.push(res.data[i])
                        }
                    }
                    this.total = res.total
                } else {
                    this.spotsList = []
                }
            }
        }
    },
}
</script>

<style lang="scss" scoped></style>